import 'package:flutter/material.dart';

class CustomDropdown<T> extends StatelessWidget {
  final String? label;
  final T? value;
  final List<DropdownMenuItem<T>>? items;
  final ValueChanged<T?>? onChanged;
  final String? hintText;
  final bool labelMark;
  final bool hiddenLabel;
  final FormFieldValidator<T>? validator;
  final EdgeInsetsGeometry? contentPadding;
  final InputBorder? border;
  final InputBorder? focusedBorder;
  final InputBorder? errorBorder;
  final Widget? suffixIcon;
  final String? Function(T)? displayText;

  const CustomDropdown({
    super.key,
    this.label,
    this.value,
    this.items,
    this.onChanged,
    this.hintText,
    this.labelMark = false,
    this.hiddenLabel = false,
    this.validator,
    this.contentPadding,
    this.border,
    this.focusedBorder,
    this.errorBorder,
    this.suffixIcon,
    this.displayText,
  });

  factory CustomDropdown.fromList({
    Key? key,
    String? label,
    T? value,
    required List<T> items,
    ValueChanged<T?>? onChanged,
    String? hintText,
    bool labelMark = false,
    bool hiddenLabel = false,
    FormFieldValidator<T>? validator,
    EdgeInsetsGeometry? contentPadding,
    InputBorder? border,
    InputBorder? focusedBorder,
    InputBorder? errorBorder,
    Widget? suffixIcon,
    required String Function(T) displayText,
  }) {
    return CustomDropdown<T>(
      key: key,
      label: label,
      value: value,
      items: items.map((item) => DropdownMenuItem<T>(value: item, child: Text(displayText(item)))).toList(),
      onChanged: onChanged,
      hintText: hintText,
      labelMark: labelMark,
      hiddenLabel: hiddenLabel,
      validator: validator,
      contentPadding: contentPadding,
      border: border,
      focusedBorder: focusedBorder,
      errorBorder: errorBorder,
      suffixIcon: suffixIcon,
    );
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!hiddenLabel) ...[
          Text.rich(
            TextSpan(
              text: label,
              children: [if (labelMark) TextSpan(text: ' *', style: TextStyle(color: theme.colorScheme.error))],
            ),
            style: theme.textTheme.bodyMedium?.copyWith(
              fontWeight: FontWeight.w500,
              color: theme.colorScheme.onSurface.withOpacity(0.8),
            ),
          ),
          const SizedBox(height: 8),
        ],
        DropdownButtonFormField<T>(
          value: value,
          items: items,
          onChanged: onChanged,
          validator: validator,
          hint:
              hintText != null
                  ? Text(
                    hintText!,
                    style: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSurface.withOpacity(0.4)),
                  )
                  : null,
          decoration: InputDecoration(
            border: border ?? const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
            enabledBorder:
                border ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                  borderSide: BorderSide(color: theme.colorScheme.outline.withOpacity(0.5)),
                ),
            focusedBorder:
                focusedBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                  borderSide: BorderSide(color: theme.colorScheme.primary, width: 1.5),
                ),
            errorBorder:
                errorBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                  borderSide: BorderSide(color: theme.colorScheme.error),
                ),
            contentPadding: contentPadding ?? const EdgeInsets.symmetric(vertical: 14, horizontal: 16),
            filled: true,
            fillColor: theme.colorScheme.surfaceContainerHighest.withOpacity(0.4),
            suffixIcon: suffixIcon,
            errorStyle: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.error, height: 1.2),
          ),
          icon: const Icon(Icons.arrow_drop_down),
          isExpanded: true,
          dropdownColor: theme.colorScheme.surface,
          style: theme.textTheme.bodyMedium,
          borderRadius: BorderRadius.circular(8),
        ),
      ],
    );
  }
}
